#include <iostream>
#include <cstring>

using namespace std;

char id[101][11];
int age[101];
int sequence[101];

void swap(int i,int j) {
    int temp = sequence[i];
    sequence[i] = sequence[j];
    sequence[j] = temp;

    temp = age[i];
    age[i] = age[j];
    age[j] = temp;

    char temp_id[11];
    strcpy(temp_id,id[i]);
    strcpy(id[i],id[j]);
    strcpy(id[j],temp_id);
}

int main() {
    int n,old_count = 0;
    cin >> n;


    // 输入数据
    for(int i = 0; i < n; i++) {
        cin >> id[i] >> age[i];
        sequence[i] = i + 1;
        if (age[i] >= 60) old_count++;
    }


    // 交换年轻人和老年人
    int l = 0,r = n -1;
    while(l < r) {
        while(age[l] >= 60) l++;
        while(age[r] < 60) r--;
        if(l < r) {
            swap(l,r);
        }
    }

    // 对老年人进行排序,如果年龄一致，则按照序号进行排序
    for(int i = 0;i < old_count;i++) {
        for (int j = 0;j < old_count - i - 1;j++) {
            if (age[j] < age[j+1]) {
                swap(j,j+1);
            }else if (age[j] == age[j+1]) {
                if (sequence[j] > sequence[j+1]) {
                    swap(j,j+1);
                }
            }
        }
    }

    // 对年轻人进行按照序号进行排序
    for(int i = old_count;i < n;i++) {
        for (int j = old_count;j <old_count + n - i - 1;j++) {
            if (sequence[j] > sequence[j+1]) {
                swap(j,j+1);
            }
        }
    }

    // 输出最后排序的结果
    for (int i = 0;i < n;i++) {
        cout << id[i] << endl;
    }

}